//
//  FondViewController.m
//  History
//
//  Created by 七 on 16/9/5.
//  Copyright © 2016年 kane. All rights reserved.
//

#import "FondViewController.h"
#import <CoreText/CoreText.h>
@interface FondViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger currentIndex;
    NSArray *fontNames;
    NSArray *fontSamples;
    __weak IBOutlet UITableView *fontTable;
    __weak IBOutlet UILabel *titleLbl;
    __weak IBOutlet UIView *navView;
    IBOutlet UIView *BGView;
    __weak IBOutlet UILabel *lineLbl;
    
    
    
    
}
@end

@implementation FondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"FontSize"] == nil) {
        currentIndex = 1;
    }else{
        currentIndex = FONT_SIZE;
    }
    fontTable.backgroundColor = KKColor_DarkWhite_Black;
    BGView.backgroundColor = KKColor_BGColor_GrayToDarkgray;
    lineLbl.backgroundColor = KKColor_NavLineColor;
    navView.backgroundColor = KKColor_DarkGray_White;
    titleLbl.textColor = KKColor_Black_LightGary;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - User Action
- (IBAction)backPress:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark UITableViewDelegate & datasource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"字体大小";
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identify = [NSString stringWithFormat:@"%ld%ld",(long)indexPath.section,(long)indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
        }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == [[[NSUserDefaults standardUserDefaults] objectForKey:@"FontSize"]integerValue]) {
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    }else{
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }
    cell.textLabel.textColor = KKColor_Black_LightGary;
    cell.backgroundColor = KKColor_DarkGray_White;
    switch (indexPath.row) {
        case 0:
            cell.textLabel.text = @"小字体";
            cell.textLabel.font = [UIFont systemFontOfSize:13];
            break;
        case 1:
            cell.textLabel.text = @"中字体";
            cell.textLabel.font = [UIFont systemFontOfSize:15];
            break;
        case 2:
            cell.textLabel.text = @"大字体";
            cell.textLabel.font = [UIFont systemFontOfSize:17];
            break;
        case 3:
            cell.textLabel.text = @"超大字体";
            cell.textLabel.font = [UIFont systemFontOfSize:19];
            break;
        default:
            break;
    }
    return cell;
}

- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
{
    
    if(indexPath.row == currentIndex){
        return UITableViewCellAccessoryCheckmark;
    }
    else{
        return UITableViewCellAccessoryNone;
    }
}


-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    currentIndex = indexPath.row;
    [fontTable reloadData];
    switch (currentIndex) {
        case 0:
            [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"FontSize"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            break;
        case 1:
            [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"FontSize"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            break;
        case 2:
            [[NSUserDefaults standardUserDefaults] setObject:@"2" forKey:@"FontSize"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            break;
        case 3:
            [[NSUserDefaults standardUserDefaults] setObject:@"3" forKey:@"FontSize"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            break;
        default:
            break;
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"fontChange" object:nil];
}

@end
